package com.person.webdemoproject2.service;



import com.person.webdemoproject2.mapper.SetAccountMapper;
import com.person.webdemoproject2.pojo.User;
import com.person.webdemoproject2.utils.jwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class SetAccountimpl implements SetAccountService {
    @Autowired
     jwtUtils jwtUtils;
    @Override
    public String login(User user) {
        User login = setAccountMapper.login(user);
        if (login != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id" ,login.getId());
            claims.put("username" ,login.getUsername());

            String jwt = jwtUtils.generateJwt(claims);
            return jwt;
        }
        return null;
    }

    @Autowired
    SetAccountMapper setAccountMapper;
    @Override
    @Transactional
    public boolean SetAccount(User user) {
        LocalDateTime now = LocalDateTime.now();
        user.setCreate_time(now);
        user.setUpdate_time(now);

        if (setAccountMapper.FindUser(user)==null){
            setAccountMapper.SetAccount(user);
            return true;
        }
        return false;
    }
}
